package cn.stylefeng.guns;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @program: football_manage
 * @description:
 * @author: xiesm
 * @create: 2023-10-20 13:50
 **/
@Slf4j
@Controller
@RequestMapping("/callback")
public class Callback2Controller {
    private String PREFIX = "/%s.html";

    /**
     * @Description: 跳转到资料库主页面
     * @Author: xiesm
     * @Date: 2019/12/31 9:20
     */
    @RequestMapping("/index")
    public String index() {
        return String.format(PREFIX, "index");
    }

    @RequestMapping("/execute")
    public String execute2(String result) {
        //URL传参过程中+号会被替换成空格，需要替换回来
        result = result.replace(" ", "+");
        //BASE64解密
        result = Base64Decoder.decodeStr(result, Charset.defaultCharset());
        log.info(result);
        return "index.html";
    }

    @SneakyThrows
    @RequestMapping("/maintenance2")
    public String execute(String result) {
        log.info("result:{}", result);
        return String.format(PREFIX, "index");
    }

    @SneakyThrows
    @RequestMapping("/maintenance3")
    public String execute(@RequestBody Map<String, Object> params) {
        log.info("params:{}", params);

        return String.format(PREFIX, "index");
    }

    @SneakyThrows
    @RequestMapping("/callback")
    public String execute(HttpServletRequest request) {
        JSONObject mapJson = null;
        InputStreamReader is = null;
        BufferedReader reader = null;
        try {
            //getInputStream只会转化request body中数据为输入流
            is = new InputStreamReader(request.getInputStream());
            reader = new BufferedReader(is);
            String str = "";
            String wholeStr = "";
            //一行一行的读取body体里面的内容；
            while ((str = reader.readLine()) != null) {
                wholeStr += str;
            }
            //接收的参数通过fastjson转化为map
            mapJson = JSONObject.parseObject(wholeStr);
        } catch (Exception e) {
            log.warn("接收参数异常。。。。。。。。{}", e.getMessage());
        } finally {
            if (is != null) {
                //关闭流
                is.close();
            }
            if (reader != null) {
                //关闭流
                reader.close();
            }
        }
        log.info("requestBody:{}", mapJson);
        return String.format(PREFIX, "index");
    }

    public static void main(String[] args) {
//        Base64Encoder.encode()
        String source = "eyJyZXN1bHQiOnsiZGF0YSI6eyJpbWciOiJodHRwOi8vZWRpcy1saXZpbmctZXMub3NzLWNuLWJlaWppbmcuYWxpeXVuY3MuY29tL2VtcHR5L2xpdmluZy1pbWcvbGY4VUhtSDE4QVRLeEg2aEJpWXVSZXp6SFMuanBnP0V4cGlyZXM9MTY5ODM5MzQ2MiZPU1NBY2Nlc3NLZXlJZD1MVEFJNXROWkpHN1J6NWljeXhDcHhETmcmU2lnbmF0dXJlPVNET1NYTmp6Qkx4WGFyUzlYc21JQmNyUTVMbyUzRCIsIm1hdGNoQ29kZSI6IjEwMDAifX0sIm1zZyI6Iuivt axguaIkOWKnyIsImNvZGUiOiIwIiwiZmVlIjoxLCJyZXFJZCI6IjdmMzZhOTQ4MWZhNDRlOGI5MjY4NjFkY2ZiZDgyOTJkIn0=".replace(" ", "+");
        String res = Base64Decoder.decodeStr(source, Charset.defaultCharset());
        System.out.println(res);
    }
}
